<?php

class Config implements Iterator {
	protected $_position = 0;
	protected $_count;
	protected $_config;
	
	public function __construct($name) {
		if (is_string($name)) {
			if (is_file($name)) {
				$config = array();
				include $name;
				$this->_config = $config;
				$this->_count = count($config);
			} else {
				
			}
		} elseif (is_array($name) || is_object($name)) {
			$this->_config = $name;
			$this->_count = count($name);
		}
	}
	
	public function get($name) {
		if (isset($this->_config[$name])) {
			$value = $this->_config[$name];
			if (is_array($value)) {
				return new Config($value);
			} else {
				return $value;
			}
		}
		return null;
	}
	
	public function __get($name) {
		return $this->get($name);
	}
	
	public function __isset($name) {
		return isset($this->_config[$name]);
	}
	
	public function current() {
		return current($this->_config);
	}
	
	public function key() {
		return key($this->_config);
	}
	
	public function next() {
		next($this->_config);
		$this->_position ++;
	}
	
	public function rewind() {
		reset($this->_config);
		$this->_position = 0;
	}
	
	public function valid() {
		return ($this->_position < $this->_count);	
	}
	
	public function toArray() {
		return $this->_config;
	}
}